/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "d3d_local.h"


//
//	ED3DRTManager::ED3DRTManager
//
ED3DRTManager::ED3DRTManager( ED3DDriver *driver )
{
	LOG_INIT("Render target manager");
	
	this->driver	=	driver;
	d3ddev		=	driver->GetDevice();
	color_surf	=	NULL;
	depth_surf	=	NULL;
	
	HRCALL( d3ddev->GetRenderTarget( 0, &color_surf ) );
	HRCALL( d3ddev->GetDepthStencilSurface( &depth_surf ) );
}


//
//	ED3DRTManager::~ED3DRTManager
//
ED3DRTManager::~ED3DRTManager( void )
{
	LOG_SHUTDOWN("Render target manager");
	
	SAFE_RELEASE(color_surf);
	SAFE_RELEASE(depth_surf);
}


//
//	ED3DRTManager::AllocRenderTarget
//
IPxTexture ED3DRTManager::AllocRenderTarget( ERSFormat_t format, int width, int height )
{
	for (uint i=0; i<rt_list.size(); i++) {
		IPxTexture	rt = rt_list[i];
		
		if (	rt.to<ED3DRenderTarget>()->GetWidth()	== width 
			&&	rt.to<ED3DRenderTarget>()->GetHeight()	== height
			&&	rt.to<ED3DRenderTarget>()->GetFormat() == format ) {

			//	'rt_list' and local 'rt' hold pointer, therefore ref count is 2.			
			if ( rt->GetRefCount() <= 2	) {
				return rt;
			}
		}
	}
	
	IPxTexture	rt = new ED3DRenderTarget(driver, format, width, height);
	
	rt_list.push_back(rt);

	return rt;
}


//
//	ED3DRTManager::RestoreRenderTargets
//
void ED3DRTManager::RestoreRenderTargets( void )
{
	HRCALL( d3ddev->SetRenderTarget( 0, color_surf ) );
	HRCALL( d3ddev->SetRenderTarget( 1, NULL ) );
	HRCALL( d3ddev->SetRenderTarget( 2, NULL ) );
	HRCALL( d3ddev->SetRenderTarget( 3, NULL ) );
	HRCALL( d3ddev->SetDepthStencilSurface( depth_surf ) );
}


//
//	ED3DRTManager::SetRenderTarget
//
void ED3DRTManager::SetRenderTarget( uint slot, IPxTexture rt )
{
	ASSERT(rt->IsRenderTarget());

	if (!rt->IsRenderTarget()) {
		SIGNAL("Can not set non-RT texture as render target");
	}
	
	HRCALL( d3ddev->SetRenderTarget( slot, rt.to<ED3DRenderTarget>()->GetSurface() ) );
}


//
//	ED3DRTManager::SetDepthStencil
//
void ED3DRTManager::SetDepthStencil( IPxTexture ds )
{
	ASSERT(ds->IsRenderTarget());

	ERSFormat_t format = ds.to<ED3DRenderTarget>()->GetFormat();
	
	if (format!=RS_FORMAT_D16 && format!=RS_FORMAT_D24_S8 && format!=RS_FORMAT_DEPTH_READABLE ) {
		SIGNAL("Non depth-stencil texture");
	}
	
	HRCALL( d3ddev->SetDepthStencilSurface( ds.to<ED3DRenderTarget>()->GetSurface() ) );
}


//
//	ED3DRTManager::ClearTarget
//
void ED3DRTManager::Clear( uint target, const EVec4 &color, float depth, uint stencil )
{
	uint clr_flags = 0;
	if ( target & RS_CLEAR_COLOR	) clr_flags |= D3DCLEAR_TARGET;
	if ( target & RS_CLEAR_DEPTH	) clr_flags |= D3DCLEAR_ZBUFFER;
	if ( target & RS_CLEAR_STENCIL	) clr_flags |= D3DCLEAR_STENCIL;

	if ( !clr_flags ) {
		return;
	}
	
	HRCALL( d3ddev->Clear(0, NULL, clr_flags, Vec4ToByte4(color), depth, stencil ) );
}


//
//	ED3DRTManager::ClearTarget
//
void ED3DRTManager::CopyBackBuffer( IPxTexture rt )
{
	if (rt->IsRenderTarget()){
		LPDIRECT3DSURFACE9 surf;
		surf = rt.to<ED3DRenderTarget>()->GetSurface();
		HRCALL( d3ddev->StretchRect(color_surf, NULL, surf, NULL,  D3DTEXF_LINEAR) );
	}
	else {
		SIGNAL("Can not copy backbuffer for non-RT texture");
	}
}


//
//	ED3DRTManager::CopyTarget
//
void ED3DRTManager::CopyTarget( IPxTexture dst, IPxTexture src )
{
	if (!dst->IsRenderTarget()){
		SIGNAL("destination buffer is not a render target");
		return;
	}

	if (!src->IsRenderTarget()){
		SIGNAL("source buffer is not a render target");
		return;
	}

	LPDIRECT3DSURFACE9 src_surf	=	src.to<ED3DRenderTarget>()->GetSurface();
	LPDIRECT3DSURFACE9 dst_surf	=	dst.to<ED3DRenderTarget>()->GetSurface();
		
	HRCALL( d3ddev->StretchRect(src_surf, NULL, dst_surf, NULL,  D3DTEXF_LINEAR) );
}